using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ForumExplorer.Extension.MSNSearch
{
    public partial class MSNSearchPanel : UserControl
    {
        public MSNSearchPanel()
        {
            this.InitializeDictionaries();
            this.InitializeComponent();
            base.AutoScaleMode = AutoScaleMode.Font;
            this.UpdateControls();
        }

        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">Created</revision>
        /// </revisionHistory>
        private System.Collections.Generic.Dictionary<string, string> msdnGroups = new System.Collections.Generic.Dictionary<string, string>();
        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">Created</revision>
        /// </revisionHistory>
        private System.Collections.Generic.IDictionary<string, string> msdnForums = new System.Collections.Generic.Dictionary<string, string>();
        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">Created</revision>
        /// </revisionHistory>
        private System.Collections.Generic.Dictionary<string, string> techNetGroups = new System.Collections.Generic.Dictionary<string, string>();
        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">Created</revision>
        /// </revisionHistory>
        private System.Collections.Generic.Dictionary<string, string> techNetForums = new System.Collections.Generic.Dictionary<string, string>();
        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">Created</revision>
        /// </revisionHistory>
        private Label forumGroupLabel;
        public Search searchControl;
        
        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">Created</revision>
        /// </revisionHistory>
        private void InitializeDictionaries()
        {
            msdnGroups.Add("", "<All Groups>");
            //			groups.Add("1", "Administrators / Moderators");
            msdnGroups.Add("14", "Site Feedback for forums.microsoft.com/msdn");
            msdnGroups.Add("12", ".NET Development");
            msdnGroups.Add("35", "Visual Studio Express Editions");
            msdnGroups.Add("11", "Smart Device Development");
            msdnGroups.Add("10", "Visual Basic");
            msdnGroups.Add("9", "Visual C#");
            msdnGroups.Add("8", "Visual C++");
            msdnGroups.Add("7", "Visual J#");
            msdnGroups.Add("17", "Visual FoxPro");
            msdnGroups.Add("6", "Visual Studio");
            msdnGroups.Add("5", "Visual Studio Team System");
            msdnGroups.Add("4", "Visual Studio Tools for Office");
            msdnGroups.Add("2", "Windows Forms");
            msdnGroups.Add("153", "Visual Studio Orcas (Pre-Release)");
            msdnGroups.Add("20", "Game Technologies:  DirectX, XNA, XACT, etc.");
            msdnGroups.Add("26", "System Center Capacity Planner");
            msdnGroups.Add("19", "SQL Server");
            msdnGroups.Add("109", "Audio and Video Development");
            msdnGroups.Add("58", "Architecture");
            msdnGroups.Add("23", "Windows Live Developer Forums");
            msdnGroups.Add("24", "Software Development for Windows Vista");
            msdnGroups.Add("234", "Windows Networking Development");
            msdnGroups.Add("25", "Windows Search Clients");
            msdnGroups.Add("18", "Microsoft ISV Community Center Forums");

            msdnForums.Add("", "<All Forums>");
            //			forums.Add("1", "Administrators");
            //			forums.Add("2", "Moderators");
            //			forums.Add("3", "Reporting Forums");
            msdnForums.Add("54", "Announcements");
            msdnForums.Add("52", "Suggestions for Forums website");
            msdnForums.Add("53", "Bug Reports for Forums website");
            msdnForums.Add("45", ".NET Framework Data Access and Storage");
            msdnForums.Add("38", "XML and the .NET Framework");
            msdnForums.Add("40", ".NET Framework Networking and Communication ");
            msdnForums.Add("41", ".NET Remoting and Runtime Serialization");
            msdnForums.Add("46", "ASP.NET Web Services and XML Serialization");
            msdnForums.Add("39", ".NET Base Class Library");
            msdnForums.Add("44", "Common Language Runtime");
            msdnForums.Add("47", "JScript for the .NET Framework");
            msdnForums.Add("42", "64-Bit .NET Framework Development.");
            msdnForums.Add("98", "FxCop Managed Code Analysis");
            msdnForums.Add("123", "LINQ Project General");
            msdnForums.Add("159", "Visual Basic Express Edition");
            msdnForums.Add("160", "Visual C# Express Edition");
            msdnForums.Add("161", "Visual C++ Express Edition");
            msdnForums.Add("24", "Installing and Registering Visual Studio Express Editions");
            msdnForums.Add("76", "Device Emulator General");
            msdnForums.Add("33", ".NET Compact Framework");
            msdnForums.Add("34", "Smart Devices General");
            msdnForums.Add("36", "Smart Devices VB and C# Projects");
            msdnForums.Add("35", "Smart Devices Native C++ Development");
            msdnForums.Add("65", "Visual Basic IDE");
            msdnForums.Add("64", "Visual Basic Language");
            msdnForums.Add("32", "Visual Basic General");
            msdnForums.Add("31", "Visual C# General");
            msdnForums.Add("59", "Visual C# Language");
            msdnForums.Add("58", "Visual C# IDE");
            msdnForums.Add("29", "Visual C++ General");
            msdnForums.Add("96", "Visual C++ Language");
            msdnForums.Add("28", "Visual J# General");
            msdnForums.Add("60", "Visual FoxPro General");
            msdnForums.Add("26", "Visual Studio Setup and Installation");
            msdnForums.Add("57", "Visual Studio Extensibility");
            msdnForums.Add("56", "Visual Studio Debugger");
            msdnForums.Add("27", "Visual Studio MSBuild");
            msdnForums.Add("25", "Visual Studio General");
            msdnForums.Add("291", "Developer Aftermarket Solutions and Powertoys");
            msdnForums.Add("50", "Visual Studio Source Control and SourceSafe");
            msdnForums.Add("55", "Visual Studio Class Designer");
            msdnForums.Add("78", "Visual Studio Guidance Automation Toolkit ");
            msdnForums.Add("75", "Visual Studio Report Controls");
            msdnForums.Add("77", "Crystal Reports for Visual Studio");
            msdnForums.Add("66", "Developer Documentation and Help System");
            msdnForums.Add("20", "Visual Studio Team System - General");
            msdnForums.Add("21", "Visual Studio Team System - Architects");
            msdnForums.Add("18", "Visual Studio Team System - Developers");
            msdnForums.Add("19", "Visual Studio Team System - Testers");
            msdnForums.Add("725", "Visual Studio Team System - Database Professionals");
            msdnForums.Add("63", "Visual Studio Team System - Microsoft Solutions Framework (MSF)");
            msdnForums.Add("61", "Visual Studio Team System - Domain-Specific Language Tools");
            msdnForums.Add("22", "Team Foundation Server - General");
            msdnForums.Add("68", "Team Foundation Server - Setup");
            msdnForums.Add("477", "Team Foundation Server - Administration");
            msdnForums.Add("481", "Team Foundation Server - Build Automation");
            msdnForums.Add("482", "Team Foundation Server - Process Templates");
            msdnForums.Add("480", "Team Foundation Server - Reporting");
            msdnForums.Add("478", "Team Foundation Server - Version Control");
            msdnForums.Add("479", "Team Foundation Server - Work Item Tracking");
            msdnForums.Add("16", "Visual Studio Tools for Office");
            msdnForums.Add("8", "Windows Forms General");
            msdnForums.Add("5", "Windows Forms Designer");
            msdnForums.Add("6", "ClickOnce and Setup & Deployment Projects");
            msdnForums.Add("7", "Windows Forms Data Controls and Databinding");
            msdnForums.Add("67", "Windows Forms Sample Applications");
            msdnForums.Add("533", "ADO.NET Technology Preview");
            msdnForums.Add("99", "Game Technologies: General");
            msdnForums.Add("106", "Game Technologies: DirectX 101");
            msdnForums.Add("100", "Game Technologies: Graphics");
            msdnForums.Add("323", "Game Technologies: XACT");
            msdnForums.Add("386", "Game Technolgies: XNA Build");
            msdnForums.Add("785", "Direct3D 10");
            msdnForums.Add("128", "Capacity Planner General ");
            msdnForums.Add("80", "SQL Server Integration Services");
            msdnForums.Add("81", "Data Mining");
            msdnForums.Add("82", "SQL Server Reporting Services");
            msdnForums.Add("83", "SQL Server Analysis Services");
            msdnForums.Add("84", "SQL Server Tools General");
            msdnForums.Add("85", "Transact-SQL");
            msdnForums.Add("86", ".NET Framework inside SQL Server");
            msdnForums.Add("87", "SQL Server Data Access");
            msdnForums.Add("88", "SQL Server SMO/DMO");
            msdnForums.Add("89", "SQL Server XML");
            msdnForums.Add("90", "SQL Server Replication");
            msdnForums.Add("91", "SQL Service Broker");
            msdnForums.Add("92", "SQL Server Security");
            msdnForums.Add("93", "SQL Server Database Engine");
            msdnForums.Add("94", "SQL Server Documentation");
            msdnForums.Add("95", "SQL Server Setup & Upgrade");
            msdnForums.Add("744", "SQL Server Disaster Recovery and Availability");
            msdnForums.Add("97", "SQL Server Notification Services");
            msdnForums.Add("152", "SQL Server Mobile");
            msdnForums.Add("158", "Getting started with SQL Server");
            msdnForums.Add("227", "Database Mirroring");
            msdnForums.Add("385", "SQL Express");
            msdnForums.Add("387", "Media Foundation Development");
            msdnForums.Add("527", "HD DVD Interactivity Authoring");
            msdnForums.Add("228", "Architecture General");
            msdnForums.Add("229", "Modeling and Tools");
            msdnForums.Add("350", "Architecture Events");
            msdnForums.Add("536", "Windows Live General: Development");
            msdnForums.Add("111", "Windows Live Search: Development");
            msdnForums.Add("112", "Windows Live Messenger: Development");
            msdnForums.Add("198", "MSN Spaces: Development");
            msdnForums.Add("534", "Windows Live Chatter: Development");
            msdnForums.Add("537", "Virtual Earth: Map Control Development");
            msdnForums.Add("646", "Windows Live ID: Development ");
            msdnForums.Add("113", "Search, Organize and Visualize in Windows Vista");
            msdnForums.Add("129", "DirectShow Development");
            msdnForums.Add("114", "Mobile PC and Tablet PC Development");
            msdnForums.Add("116", "Security for Applications in Windows Vista");
            msdnForums.Add("117", "UI Development for Windows Vista");
            msdnForums.Add("118", "Windows Communication Foundation (\"Indigo\")");
            msdnForums.Add("119", "Windows Presentation Foundation (\"Avalon\")");
            msdnForums.Add("120", "General Windows Vista Development Issues");
            msdnForums.Add("121", "Windows SDK forum");
            msdnForums.Add("122", "Windows Workflow Foundation");
            msdnForums.Add("126", "XML Paper Specification (XPS)");
            msdnForums.Add("169", "WPF Designer (\"Cider\")");
            msdnForums.Add("351", "Vista Pro-Audio Application Development");
            msdnForums.Add("352", "Microsoft UI Automation");
            msdnForums.Add("388", "Transactions Programming");
            msdnForums.Add("490", "Optical Platform Discussion");
            msdnForums.Add("784", "Windows CardSpace (\"InfoCard\")");
            msdnForums.Add("825", "Network Quality of Service (QoS/qWAVE)");
            msdnForums.Add("127", "Windows Desktop Search Development");
            msdnForums.Add("847", "Windows Desktop Search Help");
            msdnForums.Add("848", "Windows Live Search Center Development");
            msdnForums.Add("849", "Windows Live Search Center Help");
            msdnForums.Add("69", "ISV Open Discussions");
            msdnForums.Add("71", "Partnering with Microsoft");
            msdnForums.Add("231", "Architecture, Tools, and Process for ISVs");
            msdnForums.Add("74", "Visual Basic for Applications (VBA) ");

            techNetGroups.Add("", "<All Groups>");
            techNetForums.Add("", "<All Forums>");
        }

        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">
        /// changed assignment of forumID to selected value of forumsComboBox.
        /// added assignment to SearchTerms.isComment field
        /// added Debug.Assert
        /// </revision>
        /// <revision author="Peter Ritchie" date="14-jul=06">
        /// Added support for forums listbox and SearchTerms.ForumIDs
        /// </revision>
        /// <revision author="Moayad Mardini [mardini]" date="14-August-2006">
        /// Change [isIDAnswerer, isIDAsker and isIDContributor]' values
        /// </revision>
        /// <revision author="Moayad Mardini [mardini]" date="17-August-2006">
        /// Validate user inputs when search with [Asker/Poster/Answerer]'s ID instead of name
        /// </revision>
        /// </revisionHistory>
        private void copyInputs()
        {
            Debug.Assert(!(commentsOnlyCheckBox.Checked && questionsOnlyCheckBox.Checked));
            this.searchControl = new Search();
            this.searchControl.Terms.UserNameAnswerer = this.userNameOrIDTextBox.Text;
            this.searchControl.Terms.FreeText = this.keywordsTextBox.Text;
            this.searchControl.Terms.UserNameAsker = this.askerNameOrIDTextBox.Text;
            this.searchControl.Terms.UserNameContributor = this.posterNameOrIDTextBox.Text;
            this.searchControl.Terms.SiteID = this.siteComboBox.Text;
            this.searchControl.Terms.isIDAnswerer = this.isIDAnswererCheckBox.Checked;
            if (this.searchControl.Terms.isIDAnswerer == true)
            {
                if (IsInteger(this.searchControl.Terms.UserNameAnswerer) == false && !String.IsNullOrEmpty(this.searchControl.Terms.UserNameAnswerer))
                {
                    if (MessageBox.Show(this, ForumExplorer.Extension.MSNSearch.Properties.Resources.InvalidAnswererIDMessage, @"Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                    {
                        this.searchControl.Terms.isIDAnswerer = false;
                    }
                }
            }
            this.searchControl.Terms.isIDAsker = this.isIDAskerCheckBox.Checked;
            if (this.searchControl.Terms.isIDAsker == true)
            {
                if (IsInteger(this.searchControl.Terms.UserNameAsker) == false && !String.IsNullOrEmpty(this.searchControl.Terms.UserNameAsker))
                {
                    if (MessageBox.Show(this, ForumExplorer.Extension.MSNSearch.Properties.Resources.InvalidAskerIDMessage, @"Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                    {
                        this.searchControl.Terms.isIDAsker = false;
                    }
                }
            }
            this.searchControl.Terms.isIDContributor = this.isIDPosterCheckBox.Checked;
            if (this.searchControl.Terms.isIDContributor == true)
            {
                if (IsInteger(this.searchControl.Terms.UserNameContributor) == false && !String.IsNullOrEmpty(this.searchControl.Terms.UserNameContributor))
                {
                    if (MessageBox.Show(this, ForumExplorer.Extension.MSNSearch.Properties.Resources.InvalidPosterIDMessage, @"Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                    {
                        this.searchControl.Terms.isIDContributor = false;
                    }
                }
            }
            if (this.helpfulCheckBox.Checked)
            {
                this.searchControl.Terms.Helpful = true;
            }
            if (this.onlyAnsweredRadioButton.Checked)
            {
                this.searchControl.Terms.IsAnswered = 1;
            }
            else if (this.onlyUnansweredRadioButton.Checked)
            {
                this.searchControl.Terms.IsAnswered = 0;
            }
            else
            {
                this.searchControl.Terms.IsAnswered = 2;
            }
            this.searchControl.Terms.GroupID = groupsComboBox.SelectedValue;
            this.searchControl.Terms.IsQuestion = questionsOnlyCheckBox.Checked;
            this.searchControl.Terms.IsComment = commentsOnlyCheckBox.Checked;
            this.searchControl.Terms.ForumIDs.Clear();
            if (this.forumsListBox.SelectedIndices.Count > 0)
            {
                foreach (System.Collections.Generic.KeyValuePair<string, string> pair in this.forumsListBox.SelectedItems)
                {
                    String text = pair.Key as String;
                    if (!String.IsNullOrEmpty(text))
                    {
                        this.searchControl.Terms.ForumIDs.Add(text);
                    }
                }
            }
        }
        /// <revisionHistory>
        /// <revision author="Moayad Mardini [mardini]" date="17-August-2006">
        /// Create this function to validate user inputs when search with [Asker/Poster/Answerer]'s ID instead of name
        /// </revision>
        /// </revisionHistory>
        private static bool IsInteger(string Value)
        {
            try
            {
                Convert.ToInt32(Value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">
        /// Added initialization of siteComboBox.SelectedIndex
        /// </revision>
        /// </revisionHistory>
        private void MSNSearchPanel_Load(object sender, EventArgs e)
        {
            siteComboBox.SelectedIndex = 0;
        }

        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">Created</revision>
        /// </revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">
        /// </revision>
        private void siteComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.groupsComboBox.DataSource = null;
            if (siteComboBox.SelectedItem as String == "MSDN")
            {
                BindingSource bs = new BindingSource();
                bs.DataSource = msdnGroups;
                this.groupsComboBox.DataSource = bs;
                bs = new BindingSource();
                bs.DataSource = msdnForums;
                forumsListBox.DataSource = bs;
            }
            else if (siteComboBox.SelectedItem as String == "TechNet")
            {
                BindingSource bs = new BindingSource();
                bs.DataSource = techNetGroups;
                this.groupsComboBox.DataSource = bs;
                forumsListBox.DataSource = bs;
                bs = new BindingSource();
                bs.DataSource = techNetForums;
                forumsListBox.DataSource = bs;
            }
            if (this.groupsComboBox.DataSource != null)
            {
                this.groupsComboBox.DisplayMember = "Value";
                this.groupsComboBox.ValueMember = "Key";
            }
            if (this.forumsListBox.DataSource != null)
            {
                this.forumsListBox.DisplayMember = "Value";
                this.forumsListBox.ValueMember = "Key";
            }
        }

        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">Created</revision>
        /// </revisionHistory>
        private void questionsOnlyCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (questionsOnlyCheckBox.Checked)
            {
                commentsOnlyCheckBox.Checked = false;
            }
            UpdateControls();
        }

        /// <summary>
        /// Updates the state of controls whose state depends on the state of other controls
        /// </summary>
        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">Created</revision>
        /// </revisionHistory>
        private void UpdateControls()
        {
            // going from question only checked to not checked.
            if (!answerFiltersGroupBox.Enabled && questionsOnlyCheckBox.Checked)
            {
                // default to all questions
                allQuestionsRadioButton.Checked = true;
            }

            answerFiltersGroupBox.Enabled = questionsOnlyCheckBox.Checked;
            onlyAnsweredRadioButton.Enabled = questionsOnlyCheckBox.Checked;
            onlyUnansweredRadioButton.Enabled = questionsOnlyCheckBox.Checked;
            allQuestionsRadioButton.Enabled = questionsOnlyCheckBox.Checked;
            if (!allQuestionsRadioButton.Enabled)
            {
                allQuestionsRadioButton.Checked = false;
            }
            if (!onlyUnansweredRadioButton.Enabled)
            {
                onlyUnansweredRadioButton.Checked = false;
            }
            if (!onlyAnsweredRadioButton.Enabled)
            {
                onlyAnsweredRadioButton.Checked = false;
            }
        }

        /// <revisionHistory>
        /// <revision author="Peter Ritchie" date="13-jul=06">Created</revision>
        /// </revisionHistory>
        private void commentsOnlyCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (commentsOnlyCheckBox.Checked)
            {
                questionsOnlyCheckBox.Checked = false;
            }
        }

        private bool processingSelections;
        private object processingSelectionsLock = new Object();

        private void forumsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            lock (processingSelectionsLock)
            {
                if (!processingSelections)
                {
                    processingSelections = true;
                    try
                    {
                        if (forumsListBox.SelectedIndices.Count > 1)
                        {
                            // ensure "<All forums" is cleared if a forum is selected
                            forumsListBox.SetSelected(0, false);
                        }
                        if (forumsListBox.SelectedIndices.Count == 0)
                        {
                            // ensure "<all forums" is selected if no forum is selected
                            forumsListBox.SetSelected(0, true);
                        }
                    }
                    finally
                    {
                        processingSelections = false;
                    }
                }
            }
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            copyInputs();
            searchButtonClicked(sender, e);
        }

        public delegate void searchButtonClickedDelegate(object sender, EventArgs e);
        public event searchButtonClickedDelegate searchButtonClicked;

    }

}
